﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Logger;
using COFE.Core;
using System.Xml.Linq;

namespace COFE.Web
{
    /// <summary>
    /// Represents a COFE.Core.Logger.Event on the web.
    /// </summary>
    public class Event : PropertyHost<IEventLog>
    {
        #region Constructor

        private Event(IEventLog eventLog)
            : base(eventLog)
        {
            _eventLog = eventLog;            
            //EventParameters = new Dictionary<string, string>();
        }

        public static Event FromEventLog(IEventLog eventLog)
        {
            return new Event(eventLog);
        }

        #endregion

        #region Methods

        /// <summary>
        /// Serialize an entry.
        /// </summary>
        /// <returns></returns>
        public XElement ToXml(bool indentLinks = false)
        {
            var entryXml = new XElement(XNames.Event,
                                 new XAttribute(XNames.ID, this.ID.ToString())//,
                                 //new XAttribute(XNames.Type,
                                 //    _eventLog.EventArgs.GetType().Name
                                 //    .Replace("ArgsExA", "")
                                 //    .Replace("Args", "")                                     
                                 //    )
                                 );

            writeMetadata(entryXml, true);
            //writeLinks(entryXml, indentLinks);
            return entryXml;
        }


        public override IEnumerable<Link> GetLinks()
        {
            //There should be no link in the Event, just EventList.
            yield break;
        }

        #endregion

        #region Data

        #endregion

        #region Public Properties

        private IEventLog _eventLog;
        public Int64 ID { get { return GetProperty(EventLogProperties.Id).ValueAsInt64; } }
        public DateTime UtcTime { get { return GetProperty(EventLogProperties.Time).ValueAsUtcDateTime; } }
        public string Sender { get { return GetProperty(EventLogProperties.Sender).ValueAsString; } }
        public string UserId { get { return GetProperty(EventLogProperties.UserId).ValueAsString; } }
        public string EventType { get { return EventArgs.GetType().ToString(); } }
        public string EventMessage { get { return EventArgs.ToString(); } }
        public EventArgs EventArgs { get { return GetProperty(EventLogProperties.Event).Value as EventArgs; } }

        #endregion

    }
}
